package com.example.meterialproject.view.behavior.demo2

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.viewpager2.widget.ViewPager2
import com.example.meterialproject.exts.name

/**
 *
 * @ClassName: ScrollRecyclerViewBehavior
 * @Author: android 超级兵
 * @CreateDate: 4/14/22$ 1:20 PM$
 * TODO
 */
class ScrollRecyclerViewBehavior(val context: Context, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<ViewPager2>(context, attrs) {
    companion object {
        private const val TAG = "ScrollRVBehavior"
    }

    override fun onLayoutChild(
        parent: CoordinatorLayout,
        child: ViewPager2,
        layoutDirection: Int
    ): Boolean {
        parent.onLayoutChild(child, layoutDirection)
        Log.e(TAG, "onLayoutChild:dependency ${child.name}")
        return super.onLayoutChild(parent, child, layoutDirection)
    }

    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: ViewPager2,
        dependency: View
    ): Boolean {
        Log.e(TAG, "layoutDependsOn:dependency ${dependency.name}")
        return dependency is AppCompatTextView
    }

    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: ViewPager2,
        dependency: View
    ): Boolean {

        Log.e(
            TAG,
            "onDependentViewChanged dependencyName:${dependency.name}" +
                    "\tbottom:${dependency.bottom}" +
                    "\ttop:${child.top}"
        )
        ViewCompat.offsetTopAndBottom(child, -(child.top - dependency.bottom))
        return false
    }
}